const bodyParser = require('koa-bodyparser');
const list = require('./cgi/list');
const proxyNetwork = require('./cgi/proxyNetwork');
const selectEnv = require('./cgi/selectEnv');
const checkAdminLogin = require('./cgi/admin/login');
const getAllAccounts = require('./cgi/admin/allAccounts');
const addAccount = require('./cgi/admin/addAccount');
const moveAccount = require('./cgi/admin/move');
const activeAccount = require('./cgi/admin/activeAccount');
const removeAccount = require('./cgi/admin/removeAccount');
const getSettings = require('./cgi/admin/getSettings');
const setJsonData = require('./cgi/admin/setJsonData');
const setRulesTpl = require('./cgi/admin/setRulesTpl');
const setEntryPatterns = require('./cgi/admin/setEntryPatterns');
const setTestRules = require('./cgi/admin/setTestRules');
const setDefaultRules = require('./cgi/admin/setDefaultRules');
const setAccountRules = require('./cgi/admin/setAccountRules');
const getAuthKey = require('./cgi/admin/getAuthKey');
const setAuthKey = require('./cgi/admin/setAuthKey');
const enableGuest = require('./cgi/admin/enableGuest');
const proxy = require('./cgi/proxy');
const getEntryRules = require('./cgi/entryRules');
const getPluginRules = require('./cgi/pluginRules');
const changePassword = require('./cgi/account/changePassword');
const patterns = require('./cgi/patterns');
const allowlist = require('./cgi/allowlist');
const openAPI = require('./openAPI');
const follow = require('./cgi/follow');
const unfollow = require('./cgi/unfollow');
const redirect = require('./cgi/redirect');
const getEnv = require('./cgi/getEnv');
const changeAdminPassword = require('./cgi/admin/changePassword');
const changeNotice = require('./cgi/admin/changeNotice');
const setSpecPattern = require('./cgi/admin/specPattern');

const defaultBodyParser = bodyParser({ formLimit: '1mb' });
const forwardCapture = async (ctx, next) => {
  ctx.url = '/capture.html';
  await next();
};

module.exports = (router) => {
  router.get('/admin.html', checkAdminLogin);
  router.all('/cgi-bin/admin/(.*)', checkAdminLogin);
  router.all('/cgi-bin/(.*)', defaultBodyParser);
  router.get('/open-api/select', defaultBodyParser, selectEnv);
  router.get('/open-api/cur-env', defaultBodyParser, getEnv);
  router.get('/open-api/get-env', defaultBodyParser, getEnv);
  router.all('/open-api/:cgiName', openAPI);
  router.get('/cgi-bin/list', list);
  router.get('/cgi-bin/cur-env', getEnv);
  router.get('/cgi-bin/get-env', getEnv);
  router.get('/cgi-bin/rules', getEntryRules);
  router.get('/cgi-bin/entry-rules', getEntryRules);
  router.get('/cgi-bin/remote-rules', getPluginRules);
  router.get('/cgi-bin/plugin-rules', getPluginRules);
  router.all('/network/(.*)', proxyNetwork);
  router.get('/data.html', forwardCapture);
  router.get('/cgi-bin/select', selectEnv);
  router.get('/cgi-bin/admin/all-accounts', getAllAccounts);
  router.post('/cgi-bin/admin/add-account', addAccount);
  router.post('/cgi-bin/admin/move', moveAccount);
  router.post('/cgi-bin/admin/active-account', activeAccount);
  router.post('/cgi-bin/admin/remove-account', removeAccount);
  router.post('/cgi-bin/admin/change-password', changeAdminPassword);
  router.post('/cgi-bin/admin/change-notice', changeNotice);
  router.post('/cgi-bin/admin/enable-guest', enableGuest);
  router.post('/cgi-bin/admin/set-entry-patterns', setEntryPatterns);
  router.get('/cgi-bin/admin/get-settings', getSettings);
  router.post('/cgi-bin/admin/set-json-data', setJsonData);
  router.post('/cgi-bin/admin/set-rules-tpl', setRulesTpl);
  router.post('/cgi-bin/admin/set-test-rules', setTestRules);
  router.post('/cgi-bin/admin/set-default-rules', setDefaultRules);
  router.post('/cgi-bin/admin/set-account-rules', setAccountRules);
  router.get('/cgi-bin/admin/get-auth-key', getAuthKey);
  router.post('/cgi-bin/admin/set-auth-key', setAuthKey);
  router.post('/cgi-bin/admin/set-spec-pattern', setSpecPattern);
  router.get('/cgi-bin/patterns', patterns);
  router.get('/cgi-bin/allowlist', allowlist);
  router.all('/account/:name/(.*)', proxy);
  router.post('/cgi-bin/account/change-password', changePassword);
  router.get('/follow', follow);
  router.get('/unfollow', unfollow);
  router.get('/redirect', redirect);
};
